/* <![CDATA[ */
    document.write('<div id="divCalendario" align="center"><\/div>');

    var h_calendario = document.getElementById("divCalendario");
    var h_data_campo;
    var s_mese = "";
    var s_anno = "";
	var s_gior = "";
	var callFunc = "";
	var noAnno = '';
	var minDate = '';
	var maxDate = '';
	var field = '';
	var eveDoc;
	
	var anno = 0;
	var mese = 0;
	var gior = 0;

	var mesiNome = Array(0,'Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
	var mesi = Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	var ianno = 1930;
	var imese = 1;
	var tgiorni = 3;

	var maxDay = 0;
	var maxMonth = 0;
	var maxYear = 0;

	var minDay = 0;
	var minMonth = 0;
	var minYear = 0;

	var htmlCal = '';
	var calChk1 = 0;
	
	function popupCalendar()
	{
		minDate = '';
		callFunc = '';
		maxDate = '';
		
		for( var i = 0; i < arguments.length; i++ ) 
		{
			switch( i )
			{
				case 0:
					field = arguments[i];
					break;
				case 1:
					eveDoc = arguments[i];
					break;
				case 2:
					maxDate = arguments[i];
					break;
				case 3:
					callFunc = arguments[i];
					break;
				case 4:
					minDate = arguments[i];
					break;
				case 5:
					if ( arguments[i] )
					{
						if ( document.getElementById(arguments[i]) )
						{
							if ( document.getElementById(arguments[i]).value != '' ) minDate = document.getElementById(arguments[i]).value;
						}
					}
					break;
			}
		}
		
        if ( h_calendario.style.visibility == "visible" )
		{
			h_calendario.style.visibility = "hidden";
			return;
		}

        var posx = 0;
        var posy = 0;
        if (!eveDoc) var e = window.event;
        if (eveDoc.pageX || eveDoc.pageY)
		{
            posx = eveDoc.pageX;
            posy = eveDoc.pageY;
        }
		else if (eveDoc.clientX || eveDoc.clientY)
		{
            posx = eveDoc.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            posy = eveDoc.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }

        /**
        * Imposta le coordinate x,y del div del calendario
        */
        h_calendario.style.top = (posy - 10) + 'px';
        h_calendario.style.left = (posx - 150) + 'px';
		
        if( field != "")
        {
			if ( document.getElementById(field) )
				h_data_campo = document.getElementById(field);
			else
				h_data_campo = document.forms[0].elements[field];

			if ( h_data_campo.value != '' )
			{
				var tDate = h_data_campo.value;
				var elem = tDate.split('/');
				gior = elem[0];
				mese = elem[1];
				anno = elem[2];
			}
		}
		
		makeCalendar();
	}
	
	function makeCalendar()
	{		
		var date = new Date();
		var curDay = date.getDate();
		var curMonth = date.getMonth() + 1;
		var curYear = date.getFullYear();
		
		ianno = 1930;
		imese = 1;
		tgiorni = 3;
		
		if ( minDate != '' )
		{
			minDay = parseInt( minDate.substr( 0, 2 ), 10 );
			minMonth = parseInt( minDate.substr( 3, 2 ), 10 );
			minYear = parseInt( minDate.substr( 6, 4 ), 10 );
		}
		else
		{
			minDay = 1;
			minMonth = 1;
			minYear = 1900;
		}
		
		if ( maxDate != '' )
		{
			maxDay = parseInt( maxDate.substr( 0, 2), 10 );
			maxMonth = parseInt( maxDate.substr( 3, 2), 10 );
			maxYear = parseInt( maxDate.substr( 6, 4), 10 );

			anno = (anno != '') ? parseInt(anno, 10) : maxYear;
			mese = (mese != '') ? parseInt(mese, 10) : maxMonth;
			gior = (gior != '') ? parseInt(gior, 10) : maxDay;
		}
		else
		{
			anno = (anno > 0) ? parseInt(anno, 10) : curYear;
			mese = (mese > 0) ? parseInt(mese, 10) : curMonth;
			gior = (gior > 0) ? parseInt(gior, 10) : curDay;
		}
				
		if ( anno == minYear && mese < minMonth )
		{
			mese = minMonth;
		}	

		var t2 = 0;
		while (ianno <= anno)
		{
			bisestile( ianno );
			
			t2 = 0;
			if (ianno < anno)
			{
				while(t2++ < 12)
				{
					tgiorni = (mesi[t2] + tgiorni) - (Math.floor((tgiorni + mesi[t2]) / 7) * 7);
				}
			}
			else
			{
				while(++t2 < mese)
				{
					tgiorni = (mesi[t2] + tgiorni) - (Math.floor((tgiorni + mesi[t2]) / 7) * 7);
				}
			}
			ianno++;
		}
		
		htmlCal = '<input type="hidden" name="noAnno" value="' + noAnno + '" />';
		htmlCal+= '<input type="hidden" name="minDate" value="' + minDate + '" />';
		htmlCal+= '<table class="cal_table_mese_anno">';
		htmlCal+= '<tr>';

		if ( noAnno == '' )
		{
			htmlCal+= '<td align="right">';
			htmlCal+= '	<select class="cal_anno" name="c_anno" id="c_anno" width="20%" onchange="selCalendar()">';
				
				if ( maxYear > 0 )
					ianno = maxYear;
				else
					ianno = curYear + 5;

				while(ianno >= minYear)
				{
					if (ianno == anno)
						htmlCal+= '<option value="' + ianno + '" selected>'+ianno+'</option>';
					else
						htmlCal+= '<option value="' + ianno + '" >' + ianno + '</option>';

					ianno--;
				}
			htmlCal+= '	</select>';
			htmlCal+= '</td>';
		}

		htmlCal+= '<td align="right">';
		htmlCal+= '	<select class="cal_mese" name="c_mese" id="c_mese" width="70%" onchange="selCalendar()">';

		if ( anno == minYear )
			cMese = minMonth;
		else
			cMese = 1;
		
		while( cMese <= 12 )
		{
			sel = '';
			if ( cMese == mese ) sel = 'selected';
			htmlCal+= ' <option value="'+cMese+'" '+sel+'>'+mesiNome[ cMese ]+'</option>';
			cMese++;
		}
		
		htmlCal+= '</select>';
		htmlCal+= '</td>';
		htmlCal+= '<td onClick="closeCalendar()">X</td>';
		htmlCal+= '</tr>';
		htmlCal+= '</table>';

		htmlCal+= '<table class="cal_tbl_giorni" align="center">';
		htmlCal+= '	<thead>';
		htmlCal+= '		<tr height="10%">';
		htmlCal+= '			<td>Lu</td>';
		htmlCal+= '			<td>Ma</td>';
		htmlCal+= '			<td>Me</td>';
		htmlCal+= '			<td>Gi</td>';
		htmlCal+= '			<td>Ve</td>';
		htmlCal+= '			<td>Sa</td>';
		htmlCal+= '			<td>Do</td>';
		htmlCal+= '		</tr>';
		htmlCal+= '	</thead>';
		htmlCal+= '	<tbody>';

		if ( anno == minYear && mese == minMonth )
			gioView = minDay;
		else
			gioView = 1;

		var mid_out1 = '';
		var mid_out2 = '';
		var first_out = '';
		var last_out = '';
		
		giorno = 1;
		t6 = 0;
		while (giorno <= mesi[mese])
		{
			t5 = 0;
			while(t5++ < 7)
			{
				mid_out1 = '';
				mid_out2 = '';
				first_out = '';
				last_out = '';
				
				calChk1 = 0;
				
				if ( noAnno == '' )
					date = sprintf("%02s/%02s/%04s",giorno, mese, anno);
				else
					date = sprintf("%02s/%02s", giorno, mese);

				if (t6 == 1 && giorno <= mesi[mese])
				{
					if ( giorno >= gioView ) calChk1 = 1;
					giorno++;
				}
				else if (t5 == tgiorni && t6==0)
				{
					if ( giorno >= gioView ) calChk1 = 1;
					giorno++;
					t6 = 1;
				}
				else if (t5 == 7 && tgiorni == 0 && t6 == 0)
				{
					if ( giorno >= gioView ) calChk1 = 1;
					giorno++;
					t6 = 1;
				}
				
				if ( calChk1 )
					mid_out1+= "<div style=\"width:98%;height:98%;\" onclick=\"insDate('"+date+"')\"> "+( giorno - 1 )+" </div>";
				else
					mid_out2+= "<div style=\"width:98%;height:98%;\"\"></div>";
						
				if (t5 == 1) first_out+= '<tr height="10%">';

				if ( (gior + 1) == giorno && mid_out1 != '' && curMonth == mese )
					first_out+= "<td class=\"cal_giorni_curr\">\n";
				else if ( mid_out1 != '' )
					first_out+= "<td class=\"cal_giorni\" >\n";
				else
					first_out+= "<td class=\"cal_giorni_vuoti\" >&nbsp;\n";

				last_out+= "</td>\n";

				if (t5 == 7) last_out+= "</tr>\n";

				htmlCal+= first_out + mid_out1 + mid_out2 + last_out;
			}
		}
		htmlCal+= '</tbody>';
		htmlCal+= '</table>';
		
		respCalendario( htmlCal );
	}

    function selCalendar() 
	{
        mese = document.getElementById("c_mese").options[ document.getElementById("c_mese").options.selectedIndex ].value;
		
		if ( document.main.noAnno.value == '' )
			anno = document.getElementById("c_anno").options[ document.getElementById("c_anno").options.selectedIndex ].value;

		makeCalendar();
    }

	function bisestile(annoBis)
	{
		if ((annoBis % 4 == 0 && annoBis % 100 != 0) || annoBis % 400 == 0)
		{
			mesi[2] = 29;
		}
		else
		{
			mesi[2] = 28;
		}
	}

    function xmlhttpPostCalendar(strURL, parameter)
    {
        var xmlHttpReq = false;
        var self = this;
        if (window.XMLHttpRequest)
            self.xmlHttpReq = new XMLHttpRequest();
		else if (window.ActiveXObject)
        	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");

        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function()
		{
            if (self.xmlHttpReq.readyState == 4)
                respCalendario(self.xmlHttpReq.responseText);
        }
        self.xmlHttpReq.send(parameter);
    }

    function respCalendario(str)
	{
        h_calendario.innerHTML = str;
		h_calendario.style.display = "block";
    }

    function selectCalendario() {
        // viene utilizzato getElmentById per evitare di dover posizionare le funzioni del calendario nei form
        s_mese = document.getElementById("c_mese").options[ document.getElementById("c_mese").options.selectedIndex ].value;
		if ( document.main.noAnno.value == '' )
			s_anno = document.getElementById("c_anno").options[ document.getElementById("c_anno").options.selectedIndex ].value;

        xmlhttpPostCalendar("Main.php","mod=FUNZIONI&do=calendario" + "&mese=" + s_mese + "&anno=" + s_anno + "&noAnno=" + document.main.noAnno.value + "&minDate=" + document.main.minDate.value );
    }

    /**
    * Apre il calendario alle coordinate del puntatore del mouse
    * @param string id del campo che dovrà contenere la data
    * @param object event
    */
	function CalendarPopUpNoAnno(field, e, maxDate, _callFunc )
	{
		noAnno = 'S';
		CalendarPopUp(field, e, maxDate, _callFunc );
	}

    function CalendarPopUp2()
	{
		for( var i = 0; i < arguments.length; i++ ) 
		{
			switch( i )
			{
				case 0:
					var field = arguments[i];
					break;
				case 1:
					var e = arguments[i];
					break;
				case 2:
					var maxDate = arguments[i];
					break;
				case 3:
					var _callFunc = arguments[i];
					break;
				case 4:
					minDate = arguments[i];
					break;
				case 5:
					if ( arguments[i] != '' )
					{
						if ( document.getElementById(arguments[i]) )
						{
							if ( document.getElementById(arguments[i]).value != '' ) minDate = document.getElementById(arguments[i]).value;
						}
					}
					break;
			}
			
		}
		CalendarPopUp(field, e, maxDate, _callFunc );
	}

    function CalendarPopUp(field, e, maxDate, _callFunc )
	{
		callFunc = _callFunc;

        if ( h_calendario.style.visibility == "visible" )
		{
			h_calendario.style.visibility = "hidden";
			return;
		}

        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)
		{
            posx = e.pageX;
            posy = e.pageY;
        }
		else if (e.clientX || e.clientY)
		{
            posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }

        /**
        * Imposta le coordinate x,y del div del calendario
        */
        h_calendario.style.top = (posy - 10) + 'px';
        h_calendario.style.left = (posx - 150) + 'px';

        if( field != "")
        {
			if ( document.getElementById(field) )
				h_data_campo = document.getElementById(field);
			else
				h_data_campo = document.forms[0].elements[field];

			if ( h_data_campo.value != "" )
			{
				var date = h_data_campo.value;
				var elem = date.split("/");
				s_gior = elem[0];
				s_mese = elem[1];
				s_anno = elem[2];
			}
		}
		//alert("mod=FUNZIONI&do=calendario&maxDate="+ maxDate + "&minDate="+ minDate +  "&gior=" + s_gior + "&mese=" + s_mese + "&anno=" + s_anno + "&noAnno=" + noAnno);
        if (h_calendario.innerHTML == "")
            xmlhttpPostCalendar("Main.php","mod=FUNZIONI&do=calendario&maxDate="+ maxDate + "&minDate="+ minDate +  "&gior=" + s_gior + "&mese=" + s_mese + "&anno=" + s_anno + "&noAnno=" + noAnno);

        h_calendario.style.display = "block";
    }

	function CalendarClear(field, setData, _callFunc)
	{
		if ( _callFunc != "" ) setTimeout( _callFunc,250);
		document.getElementById(field).value = setData;
	}

    function insDate(gio)
	{
// 		h_data_campo.disabled = true;
        h_data_campo.value = gio;
//         h_data_campo.focus();

		if ( callFunc != "" ) setTimeout( callFunc,250);

		closeCalendar();
    }

    function closeCalendar()
	{
        h_calendario.style.display = "none";

// 		if ( document.getElementById("appDatiPrest") )
// 		{
// 			checkPrestazioni();
// 		}
    }
    

/**
*
*  Javascript sprintf
*  http://www.webtoolkit.info/
*
*
**/

sprintfWrapper = {
 
	init : function () {
 
		if (typeof arguments == "undefined") { return null; }
		if (arguments.length < 1) { return null; }
		if (typeof arguments[0] != "string") { return null; }
		if (typeof RegExp == "undefined") { return null; }
 
		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;
 
		while (match = exp.exec(string)) {
			if (match[9]) { convCount += 1; }
 
			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
 
			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount]) < 0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);
 
		if (matches.length == 0) { return string; }
		if ((arguments.length - 1) < convCount) { return null; }
 
		var code = null;
		var match = null;
		var i = null;
 
		for (i=0; i < matches.length; i++) {
 
			if (matches[i].code == '%') { substitution = '%' }
			else if (matches[i].code == 'b') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'c') {
				matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'd') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'f') {
				matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'o') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 's') {
				matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'x') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'X') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
			}
			else {
				substitution = matches[i].match;
			}
 
			newString += strings[i];
			newString += substitution;
 
		}
		newString += strings[i];
 
		return newString;
 
	},
 
	convert : function(match, nosign){
		if (nosign) {
			match.sign = '';
		} else {
			match.sign = match.negative ? '-' : match.sign;
		}
		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l < 0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == "0" || nosign) {
				return match.sign + pad + match.argument;
			} else {
				return pad + match.sign + match.argument;
			}
		} else {
			if (match.pad == "0" || nosign) {
				return match.sign + match.argument + pad.replace(/0/g, ' ');
			} else {
				return match.sign + match.argument + pad;
			}
		}
	}
}

sprintf = sprintfWrapper.init;

/* ]]> */
